package cn.kicsy.www.anonymous_bbs.service;

import cn.kicsy.www.anonymous_bbs.entity.Ban;
import cn.kicsy.www.anonymous_bbs.repository.BanRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

@Service
public class BanService {
    
    @Autowired
    private BanRepository banRepository;
    
    /**
     * 检查IP地址的封禁状态
     * @param ipAddress 要检查的IP地址
     * @return 封禁状态响应
     */
    public BanStatusResponse checkBanStatus(String ipAddress) {
        boolean isBanned = isBanned(ipAddress);
        String reason = isBanned ? getBanReason(ipAddress) : null;
        return new BanStatusResponse(isBanned, reason);
    }
    
    /**
     * 检查IP地址是否被封禁
     * @param ipAddress 要检查的IP地址
     * @return 是否被封禁
     */
    public boolean isBanned(String ipAddress) {
        if (ipAddress == null) {
            return false;
        }
        
        Ban ban = banRepository.findByIpAddress(ipAddress);
        if (ban == null) {
            return false;
        }
        
        // 检查封禁是否已过期
        if (ban.isExpired()) {
            // 如果已过期，删除该封禁记录
            banRepository.delete(ban);
            return false;
        }
        
        return true;
    }
    
    /**
     * 获取封禁原因
     * @param ipAddress 要检查的IP地址
     * @return 封禁原因，如果未被封禁则返回null
     */
    public String getBanReason(String ipAddress) {
        if (ipAddress == null) {
            return null;
        }
        
        Ban ban = banRepository.findByIpAddress(ipAddress);
        if (ban == null || ban.isExpired()) {
            return null;
        }
        
        return ban.getReason();
    }
    
    /**
     * 永久封禁IP地址
     * @param ipAddress 要封禁的IP地址
     * @param reason 封禁原因
     */
    public void banPermanently(String ipAddress, String reason) {
        banTemporarily(ipAddress, reason, null);
    }
    
    /**
     * 临时封禁IP地址
     * @param ipAddress 要封禁的IP地址
     * @param reason 封禁原因
     * @param hours 封禁小时数，null表示永久封禁
     */
    public void banTemporarily(String ipAddress, String reason, Integer hours) {
        if (ipAddress == null) {
            return;
        }
        
        Ban ban = banRepository.findByIpAddress(ipAddress);
        if (ban == null) {
            ban = new Ban();
        }
        
        ban.setIpAddress(ipAddress);
        ban.setReason(reason);
        
        if (hours != null && hours > 0) {
            ban.setExpiresAt(LocalDateTime.now().plus(hours, ChronoUnit.HOURS));
        } else {
            ban.setExpiresAt(null); // 永久封禁
        }
        
        banRepository.save(ban);
    }
    
    /**
     * 解除对IP地址的封禁
     * @param ipAddress 要解除封禁的IP地址
     */
    public void unban(String ipAddress) {
        if (ipAddress == null) {
            return;
        }
        
        Ban ban = banRepository.findByIpAddress(ipAddress);
        if (ban != null) {
            banRepository.delete(ban);
        }
    }
}